package com.yz.app.sale.api;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yz.app.sale.base.BaseApp;
import com.yz.app.sale.utils.CacheInterceptor;
import com.yz.app.sale.utils.FileUtils;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * ----------BigGod be here!----------/
 * ***┏┓******┏┓*********
 * *┏━┛┻━━━━━━┛┻━━┓*******
 * *┃             ┃*******
 * *┃     ━━━     ┃*******
 * *┃             ┃*******
 * *┃  ━┳┛   ┗┳━  ┃*******
 * *┃             ┃*******
 * *┃     ━┻━     ┃*******
 * *┃             ┃*******
 * *┗━━━┓     ┏━━━┛*******
 * *****┃     ┃神兽保佑*****
 * *****┃     ┃代码无BUG！***
 * *****┃     ┗━━━━━━━━┓*****
 * *****┃              ┣┓****
 * *****┃              ┏┛****
 * *****┗━┓┓┏━━━━┳┓┏━━━┛*****
 * *******┃┫┫****┃┫┫********
 * *******┗┻┛****┗┻┛*********
 * ━━━━━━神兽出没━━━━━━
 *
 * 项目名称：Bj1601Projects
 * 类描述：
 * 创建人：zhangwei
 * 创建时间：2016-06-24 10:37
 * 修改人：zhangwei
 * 修改时间：2016-06-24 10:37
 * 修改备注：
 */
public class ApiStore {
    private static final long DEFAULT_JSON_CACHE_SIZE = 250 * 1024 * 1204;

    private static volatile ApiStore instance = null;
    private static Retrofit retrofit;

    private ApiStore() {
    }

    public static ApiStore getInstance() {
        if (instance == null) {
            synchronized (ApiStore.class) {
                if (instance == null) {
                    instance = new ApiStore();
                }
            }
        }
        return instance;
    }

    public ApiService getApiService( ) {
        return getRetrofit().create(ApiService.class);
    }

    public Retrofit getRetrofit() {
        OkHttpClient client = new OkHttpClient.Builder()
                .cache(new Cache(FileUtils.getJsonCache(BaseApp.app), DEFAULT_JSON_CACHE_SIZE))
                .addInterceptor(new CacheInterceptor(BaseApp.app))
                .build();
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(UrlConstant.Request.SMZDM_BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(getGson()))
                    .build();
        }
        return retrofit;
    }

    /**
     * java bean 转化json数据  序列化
     * 解析反序列化
     *
     * @return
     */
    public Gson getGson() {
        return new GsonBuilder()
                .serializeNulls()//允许序列化反序列化为null
                .create();
    }


}
